package com.way.app;

import java.util.ArrayList;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.way.drag_gridview.R;
import com.way.view.DraggableGridView;

@SuppressLint("NewApi")
public class MyActivity extends Activity {
	private static Random mRandom = new Random();
	private static String[] mWords = "我 是 一 只 大 笨 猪".split(" ");
	private ArrayList<String> mPoem = new ArrayList<String>();
	private DraggableGridView mDragView;
	private LayoutInflater mInflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mDragView = (DraggableGridView) findViewById(R.id.grid);
		mInflater = LayoutInflater.from(this);
		setConfigs();
		genTiles();
		setListeners();
	}

	private void setListeners() {
		mDragView
				.setOnRearrangeListener(new DraggableGridView.OnRearrangeListener() {
					public void onRearrange(int oldIndex, int newIndex) {
						String oldTile = mPoem.get(oldIndex);
						mPoem.remove(oldIndex);
						mPoem.add(newIndex, oldTile);
					}

					@Override
					public void onDelete(int index) {
						mPoem.remove(index);
					}
				});
		mDragView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == mDragView.getChildCount() - 1) {
					mPoem.add(mWords[mRandom.nextInt(mWords.length)]);
					addTile(mPoem.get(mPoem.size() - 1),
							R.drawable.ic_launcher, 0, true);
					return;
				}

				if (arg2 < mDragView.getChildCount() - 1 && arg2 >= 0) {
					Toast.makeText(MyActivity.this, mPoem.get(arg2),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

	private void setConfigs() {

		mDragView.setCellHeight(getResources().getDimensionPixelSize(
				R.dimen.quick_settings_cell_height));

		mDragView.setCellGap(getResources().getDimensionPixelSize(
				R.dimen.quick_settings_cell_gap));

		mDragView.setColumnCount(getResources().getInteger(
				R.integer.quick_settings_num_columns));
	}

	private void genTiles() {
		mDragView.removeAllViews();
		for (int i = 0; i < 10; i++) {
			mPoem.add(mWords[mRandom.nextInt(mWords.length)]);
			addTile(mPoem.get(i), R.drawable.ic_launcher, 0, false);
		}
		addTile(getResources().getString(R.string.profiles_add), 0,
				R.drawable.ic_menu_add, false);
	}

	private void addTile(String title, int iconSysId, int iconRegId,
			boolean newTile) {
		View tileView = null;
		if (iconRegId != 0) {
			tileView = (View) mInflater.inflate(
					R.layout.quick_settings_tile_generic, null, false);
			final TextView name = (TextView) tileView
					.findViewById(R.id.tile_textview);
			name.setText(title);
			name.setCompoundDrawablesRelativeWithIntrinsicBounds(0, iconRegId,
					0, 0);
		} else {
			tileView = (View) mInflater.inflate(
					R.layout.quick_settings_tile_user, null, false);
			ImageView iv = (ImageView) tileView
					.findViewById(R.id.user_imageview);
			TextView tv = (TextView) tileView.findViewById(R.id.tile_textview);
			tv.setText(title);
			iv.setImageResource(iconSysId);

		}
		mDragView.addView(tileView, newTile ? mDragView.getChildCount() - 1
				: mDragView.getChildCount());
	}
}
